<?php
/*
RPHP Router
@category  Core
*/
class RPHP_Router
{
	
	protected static $_instance = null;
	public $u;
	public $_model;
	public $_action;
	public $defaultRule = array(
		'model'		=> 'index',
        'action'    => 'index',
    );
	public $defaultTemplate = "index";
	public $staticName = ".html";
	
	
	public function __construct()
	{		
		$this->u = parse_url($_SERVER['REQUEST_URI']);		
		$this->u['path'] = substr(str_replace("//","/",$this->u['path']),1,strlen($this->u['path'])-1);
	}
	
	public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
	
	public function dynamicMatch()
    {
        $this->_model = $this->defaultRule['model'];
		$this->_action = $this->defaultRule['action'];

        $tmp = explode('/', $this->u['path']);
		$model = current($tmp);
        if ( preg_match("/^([a-zA-Z0-9\_\-]{1,20})$/",$model )  ) {
            $this->_model = $model;
        }		
		
		$action = next($tmp);
        if ( preg_match("/^([a-zA-Z0-9\_\-]{1,20})$/", $action )) {
            $this->_action = $action;
        }

		$para = end($tmp);
		if( strpos($para,$this->staticName) ){
			$para = explode("-", substr($para,0,-5) );
			while (false !== ($next = next($para))) {
				if(!$this->_para) $next=prev($para);
				$this->_para[$next] = urldecode(next($para));
			}
		
		}
		return array(
				'model'=>$this->_model,
				'controller'=>$this->_model.'Controller',
				'action'=>$this->_action,
				'deftpl'=>$this->_action,
				'para'=>$this->_para
				);
      
    }
	
	
	
	public function __set($key, $value = null)
    {
        $this->$key = $value;
    }
	
	
}
?>